import * as types from './types'
import {GetChaptersApi} from '../../config/api'

export const fetch_chapters = (textbook_id) => {
    return dispatch => {
        dispatch({type: types.FETCHING_CHAPTERS})
        GetChaptersApi(textbook_id).then((data) => {
            if (data.code === 0) {
                dispatch({
                    type: types.FETCH_CHAPTERS_SUCCESS,
                    name: data.name,
                    cover_url: data.cover_url,
                    chapters: data.sections,
                    description: data.description,
                    learning_count: data.learning_count
                })
            } else {
                dispatch({type: types.FETCH_CHAPTERS_FAIL})
            }
        }).catch((err) => {
            dispatch({type: types.FETCH_CHAPTERS_FAIL})            
        })
    }
}